﻿from pao.reports.project import projectManagerReport

def buildPath(request, group):
    
    url = request.path
    departmentId = request.user.employee.department.id
    username = request.user.username
    result = []
    if hasRights(group,"Enterprise"):
        result += [{'url':'/enterprise/manager', 'value':'Pagrindinis'}]
    elif hasRights(group,"DepartmentManager"):
        result += [{'url':'/department/manager/' + str(departmentId), 'value':'Pagrindinis'}]
    elif hasRights(group,"ProjectManager"):
        result += [{'url':'/project/list/' + username, 'value':'Pagrindinis'}]
    elif hasRights(group,"EmployeeManager"):
        result += [{'url':'/personal/' + username, 'value':'Pagrindinis'}]
    current = ""
    if url.find("personal")!= -1:
        current = 'Vykdomi projektai'
        #when in lower level
        #if group != 1:
        #when watching other users
        if url.find(username) != -1:
            current += ' (mano)'
        else:
            current += ' ('+url[url.rfind('/')+1:len(url)]+')'
    elif url.find("project/list")!= -1:
        current = "Vadovaujami projektai"
        if url.find(username) != -1:
            current += ' (mano)'
        else:
            current += ' ('+url[url.rfind('/')+1:len(url)]+')'
        #when watching other users
        #if url.find(username) != -1:
    elif url.find("project/manager")!= -1:
        projectId = int(url[url.rfind('/')+1:len(url)])
        current = "Projekto ataskaita ("+ str(projectId) +")"
        if request.session.has_key('lastPath') == True and request.session["lastPath"].find('department/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Skyriaus projektai'}]
        elif request.session.has_key('lastPath') == True and request.session["lastPath"].find('enterprise/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Firmos projektai'}]
        else: 
            managerName = projectManagerReport(projectId)["manager"]
            if managerName == username:
                nameVisible = 'mano'
            else:
                nameVisible = managerName        
            result += [{'url':'/project/list/' + managerName, 'value':'Vadovaujami projektai ('+nameVisible+')'}]
    elif url.find("project/intensity")!= -1:
        projectId = int(url[url.find('/',url.find("project/intensity")+len("project/intensity"))+1:url.find('/',url.find("project/intensity")+len("project/intensity")+1)])
        current = "Projekto intensyvumo atasakaita (" + str(projectId) + ")"
        if request.session.has_key('lastPath') == True and request.session["lastPath"].find('department/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Skyriaus projektai'}]
        elif request.session.has_key('lastPath') == True and request.session["lastPath"].find('enterprise/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Firmos projektai'}]
        else:
            
            managerName = projectManagerReport(projectId)["manager"]
            if managerName == username:
                nameVisible = 'mano'
            else:
                nameVisible = managerName  
            result += [{'url':'/project/list/' + managerName, 'value':'Vadovaujami projektai ('+nameVisible+')'}]
    elif url.find("project/budget")!= -1:
        projectId = int(url[url.find('/',url.find("project/budget")+len("project/budget"))+1:url.find('/',url.find("project/budget")+len("project/budget")+1)])
        current = "Projekto biudžeto ataskaita (" + str(projectId) + ")"
        if request.session.has_key('lastPath') == True and request.session["lastPath"].find('department/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Skyriaus projektai'}]
        elif request.session.has_key('lastPath') == True and request.session["lastPath"].find('enterprise/projects') != -1:
            result += [{'url':request.session['lastPath'], 'value':'Firmos projektai'}]
        else:   
            managerName = projectManagerReport(projectId)["manager"]
            if managerName == username:
                nameVisible = 'mano'
            else:
                nameVisible = managerName   
            result += [{'url':'/project/list/' + managerName, 'value':'Vadovaujami projektai ('+nameVisible+')'}]

    elif url.find("department/manager")!= -1:
        current = "Skyriaus ataskaita"
    elif url.find("department/budget")!= -1:
        current = "Biudžeto vykdymo ataskaita"
    elif url.find("department/employees")!= -1:
        current = "Skyriaus darbuotojai"
    elif url.find("department/projects")!= -1:
        current = "Skyriaus projektai"
    elif url.find("enterprise/project")!= -1:
        current = "Firmos projektai"
    elif url.find("enterprise/unexecuted")!= -1:
        current = "Neįvykdyti projektai"
    elif url.find("enterprise/budget")!= -1:
        current = "Biudžeto vykdymo ataskaita"
    elif url.find("enterprise/manager")!= -1:
        current = "Skyrių ataskaita"
    elif url.find("enterprise/employees")!= -1:
        current = "Firmos darbuotojai"
    elif url.find("enterprise/load")!= -1:
        current = "Darbuotojų ir skyrių apkrovimo ataskaita"
    result += [{'url':url, 'value':current}]
    return result
    
def hasRights(group, right):
    for entry in group:
        if entry == right:
            return True
    return False
